<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="trendName">
        <el-input
          v-model="queryParams.trendName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="trendType">
        <el-input v-model="queryParams.trendType" placeholder="请输入类型" clearable size="small">
        </el-input>
      </el-form-item>
      <el-form-item label="园区编号" prop="parkNum">
        <el-input
          v-model="queryParams.parkNum"
          placeholder="请输入园区编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:trendType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:trendType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:trendType:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:trendType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trendTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="编号" align="center" prop="trendId" />-->
      <el-table-column label="名称" align="center" prop="trendName" />
      <el-table-column label="类型" align="center" prop="trendType" />
      <el-table-column label="状态0正常" align="center" prop="status" />
      <el-table-column label="步长" align="center" prop="size" />
      <el-table-column label="步长单位" align="center" prop="sizeUnit" />
      <el-table-column label="园区编号" align="center" prop="parkNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:trendType:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:trendType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改趋势图管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="trendName">
          <el-input v-model="form.trendName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="trendType">
          <el-input v-model="form.trendType" placeholder="请选择类型">
          </el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="步长" prop="size">
          <el-input v-model="form.size" placeholder="请输入步长" />
        </el-form-item>
        <el-form-item label="步长单位" prop="sizeUnit">
          <el-input v-model="form.sizeUnit" placeholder="请输入步长单位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="园区编号" prop="parkNum">
          <el-input v-model="form.parkNum" placeholder="请输入园区编号" />
        </el-form-item>
        <el-divider content-position="center">趋势图数据信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTrendData">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTrendData">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="trendDataList" :row-class-name="rowTrendDataIndex" @selection-change="handleTrendDataSelectionChange" ref="trendData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
<!--          <el-table-column label="排序" prop="dataSort">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dataSort" placeholder="请输入排序" />
            </template>
          </el-table-column>-->
          <el-table-column label="标签" prop="dataLabel">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dataLabel" placeholder="请输入标签" />
            </template>
          </el-table-column>
          <el-table-column label="值" prop="dataValue">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dataValue" placeholder="请输入值" />
            </template>
          </el-table-column>
<!--          <el-table-column label="默认值" prop="defaultValue">
            <template slot-scope="scope">
              <el-input v-model="scope.row.defaultValue" placeholder="请输入默认值" />
            </template>
          </el-table-column>
          <el-table-column label="状态0正常" prop="status">
            <template slot-scope="scope">
              <el-input v-model="scope.row.status" placeholder="请输入状态" />
            </template>
          </el-table-column>-->
<!--          <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>-->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTrendType, getTrendType, delTrendType, addTrendType, updateTrendType } from "@/api/system//trend/trendType";

export default {
  name: "TrendType",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTrendData: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 趋势图管理表格数据
      trendTypeList: [],
      // 趋势图数据表格数据
      trendDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trendName: null,
        trendType: null,
        status: null,
        size: null,
        sizeUnit: null,
        parkNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        trendName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        trendType: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        parkNum: [
          { required: true, message: "园区编码不能为空", trigger: "blur" }
        ],
        size: [
          { required: true, message: "步长不能为空", trigger: "blur" }
        ],
        sizeUnit: [
          { required: true, message: "步长单位不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询趋势图管理列表 */
    getList() {
      this.loading = true;
      listTrendType(this.queryParams).then(response => {
        this.trendTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        trendId: null,
        trendName: null,
        trendType: null,
        status: "0",
        size: null,
        sizeUnit: null,
        remark: null,
        parkNum: 'A',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.trendDataList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.trendId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加趋势图管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const trendId = row.trendId || this.ids
      getTrendType(trendId).then(response => {
        this.form = response.data;
        this.trendDataList = response.data.trendDataList;
        this.open = true;
        this.title = "修改趋势图管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.trendDataList = this.trendDataList;
          if (this.form.trendId != null) {
            updateTrendType(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrendType(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const trendIds = row.trendId || this.ids;
      this.$confirm('是否确认删除趋势图管理编号为"' + trendIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTrendType(trendIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
	/** 趋势图数据序号 */
    rowTrendDataIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 趋势图数据添加按钮操作 */
    handleAddTrendData() {
      let obj = {};
      obj.dataSort = "";
      obj.dataLabel = "";
      obj.dataValue = "";
      obj.defaultValue = "";
      obj.status = "";
      obj.remark = "";
      this.trendDataList.push(obj);
    },
    /** 趋势图数据删除按钮操作 */
    handleDeleteTrendData() {
      if (this.checkedTrendData.length == 0) {
        this.$alert("请先选择要删除的趋势图数据数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.trendDataList.splice(this.checkedTrendData[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleTrendDataSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.trendData.clearSelection();
        this.$refs.trendData.toggleRowSelection(selection.pop());
      } else {
        this.checkedTrendData = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/trendType/export', {
        ...this.queryParams
      }, `system_trendType.xlsx`)
    }
  }
};
</script>
